package com.cmc.cloud.cmclink.doc.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.rpcservice.CustomerRpcService;
import com.cmc.cloud.cmclink.doc.vo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import java.util.*;


import com.cmes.framework.common.pojo.PageResult;

import com.cmc.cloud.cmclink.doc.entity.TblisustampMainDO;
import com.cmc.cloud.cmclink.doc.service.TblisustampMainService;


/**
 * {table.classComment}前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 申请主信息")
@RestController
@RequestMapping(UrlConstants.APPLICATION_URL+"/stampMain")
@Validated
public class TblisustampMainController {

    @Resource
    private TblisustampMainService tblisustampMainService;

    @Resource
    private CustomerRpcService nameTransferService;

    @Operation(summary = "创建申请主信息")
    @PostMapping("/create")
    public CommonResult<Void> createTblisustampMain(@Valid @RequestBody TblisustampMainCreateReqVO createReqVO) {
        tblisustampMainService.createTblisustampMain(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新申请主信息")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:B/LIssuingStampApprove:edit')")
    public CommonResult<Void> updateTblisustampMain(@Valid @RequestBody TblisustampMainUpdateReqVO updateReqVO) {
        tblisustampMainService.updateTblisustampMain(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除申请主信息")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:B/LIssuingStampApprove:delete')")
    public CommonResult<Void> deleteTblisustampMain(@PathVariable("id") Long id) {
        tblisustampMainService.deleteTblisustampMain(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得申请主信息")
    @GetMapping("/getById/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TblisustampMainRespVO> getTblisustampMain(@PathVariable("id") Long id) {
        TblisustampMainDO tblisustampMain = tblisustampMainService.getTblisustampMain(id);
        return CommonResult.success(BeanUtil.copyProperties(tblisustampMain, TblisustampMainRespVO.class));
    }

    @Operation(summary = "获得申请主信息列表")
    @GetMapping("/listAll")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<TblisustampMainRespVO>> getTblisustampMainList(@RequestParam("ids") Collection<Long> ids) {
        List<TblisustampMainDO> list = tblisustampMainService.getTblisustampMainList(ids);
        return CommonResult.success(BeanUtil.copyToList(list,TblisustampMainRespVO.class));
    }

    @Operation(summary = "获得申请主信息分页")
    @PostMapping("/getPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:B/LIssuingStampApprove:search')")
    public CommonResult<PageResult<TblisustampMainRespVO>> getTblisustampMainPage(@Valid @RequestBody TblisustampMainPageReqVO pageVO) {
        PageResult<TblisustampMainDO> pageResult = tblisustampMainService.getTblisustampMainPage(pageVO);
        List<TblisustampMainRespVO> results = BeanUtil.copyToList(pageResult.getList(),TblisustampMainRespVO.class);
/*        nameTransferService.fillCompanyNameFunctionally(
                results,
                // 提取bkpCode的函数
                TblisustampMainRespVO::getAgentCode,
                // 设置本地名称的函数
                TblisustampMainRespVO::setCompanyNameLocal,
                // 设置英文名称的函数
                TblisustampMainRespVO::setCompanyNameEn
        );*/
        return CommonResult.success(new PageResult<>(results,pageResult.getTotal()));
    }

    @Operation(summary = "获得待审批页面详情")
    @PostMapping("/getMainFilesPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:B/LIssuingStampRetrieve:search')")
    public CommonResult<PageResult<TblisustampMainFilesVO>> getMainFilesPage(@Valid @RequestBody TblisustampMainPageReqVO pageVO) {
        PageResult<TblisustampMainFilesVO> pageResult = tblisustampMainService.getMainFilesPage(pageVO);
        List<TblisustampMainFilesVO> tblisustampMainFilesVos = pageResult.getList();
        if(CollectionUtil.isNotEmpty(tblisustampMainFilesVos)){
            nameTransferService.fillAgentName(tblisustampMainFilesVos);
        }
        return CommonResult.success(new PageResult<>(tblisustampMainFilesVos, pageResult.getTotal()));
    }


}
